package com.starbaba.weixin.view;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.starbaba.comment.model.EntityVO;
import com.starbaba.core.service.CommonService;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.weixin.constants.WeixinMsgConstant;
import com.starbaba.weixin.model.WeixinMsgModelListVO;
import com.starbaba.weixin.model.WeixinMsgModelVO;

public class WeixinMsgAction extends BaseAction implements ModelDriven<WeixinMsgModelVO>{

	/**
	 * 
	 */
	private static final long serialVersionUID = -7098842831316562005L;
	
	private WeixinMsgModelVO modelVO;
	private List<EntityVO> bussinessEntityVOs;
	
	public String showPage() {
		getBusinessCategory();
		return ActionSupport.SUCCESS;
	};
	
	public WeixinMsgAction() {
		this.voClass = WeixinMsgModelVO.class;
		this.entityListVO = new WeixinMsgModelListVO();
		this.pkNameArray = new String[] { "id" };
	}

	@Override
	public WeixinMsgModelVO getModel() {
		return modelVO;
	}

	
	@Override
	protected void queryData() throws Exception {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(
				Struts2Utils.getRequest(), paramsMap);
		String business = (String) paramsMap.get(WeixinMsgConstant.BUSINESS);
		String reply = (String) paramsMap.get(WeixinMsgConstant.REPLY);
		String message = (String) paramsMap.get(WeixinMsgConstant.MESSAGE);
		String beginDate = (String) paramsMap.get(WeixinMsgConstant.BEGIN_DATE);
		String endDate = (String) paramsMap.get(WeixinMsgConstant.END_DATE);
		String filterMesaage = (String) paramsMap.get(WeixinMsgConstant.FILTER_MESAAGE);
		
		WeixinMsgModelListVO listVO = (WeixinMsgModelListVO) this.entityListVO;
		if (business != null) {
			if (business.equals("未处理业务")) {
				business = " ";
			}
			listVO.set_seq_business(business);
		}
		if (reply != null) {
			listVO.set_sk_reply(reply);
		}
		if (message != null) {
			listVO.set_sk_message(message);
		}
		if (beginDate != null) {
			beginDate = beginDate + " 00:00:00" ;
			listVO.set_dge_date(beginDate);
			
		}
		if (endDate != null) {
			endDate = endDate + " 23:59:59";
			listVO.set_dle_date(endDate);
		}
		if (filterMesaage != null) {
			filterMesaage = "%" + filterMesaage + "%";
			listVO.set_snk_message(filterMesaage);
		}
		listVO.setOrderBy("date desc, business, message, reply");
		
		page = this.getService().doQuery(WeixinMsgModelVO.class, listVO);
	}
	
	private void getBusinessCategory() {
		StringBuffer sb = new StringBuffer();
		sb.append("select distinct business from loan.weixin_mpmsg_record");
		CommonService service = getService();
		List businessCategoryList = service.doFindBySQL(sb.toString(),
				null, null);
		bussinessEntityVOs =  new ArrayList<EntityVO>();
		for (int i = 0; i < businessCategoryList.size();i++) {
			String s = (String) businessCategoryList.get(i);
			String key = s;
			if (s.equals("")) {
				s = "未处理业务";
				key = "未处理业务";
			}
			EntityVO entityVO = new EntityVO(key, s);
			bussinessEntityVOs.add(entityVO);
		}
		paramsMap.put(WeixinMsgConstant.BUSINESS_LIST, bussinessEntityVOs);
	}
}
